package com.zelin.web.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/10/18-15:25
 * ------------------------------
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1. 得到session
        HttpSession session = request.getSession();
        //2. 判断session中的username属性是否有值
        Object username = session.getAttribute("username");
        //3. 判断
        if(username == null){  //① 如果为null，代表未登录
            response.sendRedirect("/login.jsp");
        }
        return true;
    }
}
